"""
按周期计算收益率，简单方法
"""
import pandas as pd
import numpy as np
from scipy.stats import percentileofscore
class box:
    def __init__(self,box=None):
        self.box=box
    def oncalc(self,d,timekey=None):
        f=d.copy()
        f.sort_values(inplace=True)
        f1=f.isna()
        n=len(f)+1
        f=pd.Series(data=list(range(1,n)),index=f.index)
        f.loc[f1] = np.nan
        if self.box:
            min0=f.min()
            max0=f.max()
            f=(f-min0)/(max0-min0)
            if self.box !=[0,1]:
                f=f*(self.box[1]-self.box[0])+self.box[0]
        return f



